package hnair.com.shopstore.demo.recycleview;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import hnair.com.shopstore.R;

/**
 * Created by liaowei on 2017/11/22.
 */

public class BaseRecycleAdapter extends RecyclerView.Adapter<BaseRecycleAdapter.BaseRecycleViewHolder> {

    private Context mContext;
    private List<String> mDatas;
    private ItemClickListener mItemClickListener;

    public BaseRecycleAdapter(Context context, List<String>datas) {
        this.mContext = context;
        this.mDatas = datas;
    }

    public void setOnItemClickListener(ItemClickListener listener) {
        this.mItemClickListener = listener;
    }

    @Override
    public BaseRecycleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //创建ViewHodler
        //布局条目的view
        View itemView = LayoutInflater.from(this.mContext).inflate(R.layout.item_home, parent, false);
        BaseRecycleViewHolder viewHolder = new BaseRecycleViewHolder(itemView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(BaseRecycleViewHolder holder, final int position) {
        //绑定数据
        holder.itemTv.setText(mDatas.get(position));

        //绑定点击事件
        if (mItemClickListener != null) {
            holder.itemTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //利用点击回调
                    mItemClickListener.onItemClick(position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        //总显示的数据条数
        return mDatas.size();
    }


    public class BaseRecycleViewHolder extends RecyclerView.ViewHolder {

        public TextView itemTv;

        public BaseRecycleViewHolder(View itemView) {
            super(itemView);
            itemTv = (TextView) itemView.findViewById(R.id.id_num);
        }

    }


    public interface ItemClickListener {
        public void onItemClick(int position);
    }
}
